<template>
    <div id="globalHeader">
        <a-row :wrap="false">
            <a-col flex="200px">
                <router-link to="/">
                    <div class="title-bar">
                        <img src="@/assets/logo.png" alt="logo" class="logo">
                        <div class="title">鱼皮云图库</div>
                    </div>
                </router-link>
            </a-col>
            <a-col flex="auto">
                <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="doMenuClick" />
            </a-col>
            <a-col flex="100px">
                <div class="user-login-status">
                    <div v-if="loginUserStore.loginUser.id">
                        <a-dropdown>
                            <ASpace>
                                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                                {{ loginUserStore.loginUser.userName ?? '无名' }}
                            </ASpace>
                            <template #overlay>
                                <a-menu>
                                    <a-menu-item @click="doLogout">
                                        <LogoutOutlined />
                                        退出登录
                                    </a-menu-item>
                                </a-menu>
                            </template>
                        </a-dropdown>
                    </div>


                    <div v-else>
                        <a-button type="primary" href="/user/login">登录</a-button>
                    </div>

                </div>
            </a-col>
        </a-row>



    </div>

</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { HomeOutlined,LogoutOutlined  } from '@ant-design/icons-vue'
import { message, type MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { userLogoutUsingGet } from '@/api/yonghuxiangguanjiekou'
const loginUserStore = useLoginUserStore()

const items = ref<MenuProps['items']>([
    {
        key: '/',
        icon: () => h(HomeOutlined),
        label: '主页',
        title: '主页',
    },
    {
        key: '/about',
        label: '关于',
        title: '关于',
    },
    {
        key: 'others',
        label: h('a', { href: 'https://www.codefather.cn', target: '_blank' }, '编程导航'),
        title: '编程导航',
    },
])

const router = useRouter();
//路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
    router.push({ path: key })
}
//当前要高亮的菜单
const current = ref<string[]>(['home'])
//通过钩子函数获取当前菜单,使其高亮
router.afterEach((to) => {
    current.value = [to.path]
})

const doLogout = async () => {
    const res = await userLogoutUsingGet()
    console.log(res)
    if (res.data.code === 0){
        //退出后必须要清空用户登录信息，把用户名设为未登录
        loginUserStore.setLoginUser({
            userName:"未登录"
        })
        message.success("退出成功")
        await router.push({path:"/user/login"})
    }else{
        message.error("退出失败"+res.data.message)
    }
    
}
</script>
<style scoped>
#globalHeader {
    .title-bar {
        display: flex;
        align-items: center;

        .logo {
            height: 48px;
            ;
        }

        .title {
            color: black;
            font-size: 18px;
            margin-left: 16px;
        }

    }

    ;
}
</style>